import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout 一次导入了layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      // meta 自定义这个路由信息
      meta: { title: '首页', icon: 'el-icon-s-home', keepAlive: true }

    }]
  },

  {
    path: '/depart',
    component: Layout,
    name: 'Depart',
    children: [
      {
        path: 'index',
        name: 'depart',
        component: () => import('@/views/depart/index.vue'),
        meta: { title: '组织', icon: 'table' }
      }
    ]
  },
  { // 员工 列表
    path: '/person',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'person',
        component: () => import('@/views/person/index.vue'),
        meta: { title: '员工', icon: 'user' }
      }
    ]
  },
  { // 员工 详情
    path: '/person',
    component: Layout,
    hidden: true, // 不生成菜单
    children: [
      {
        path: 'detail',
        name: 'perdetail',
        component: () => import('@/views/person/child/personDetail.vue'),
        meta: { title: '添加员工', icon: 'user' }
      }
    ]
  },
  { // 审批模块
    path: '/approval',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'approval',
        component: () => import('@/views/approval/index.vue'),
        meta: { title: '审批', icon: 'user' }
      }
    ]
  },
  { // 审批详情
    path: '/approval',
    component: Layout,
    hidden: true, // 不生成菜单
    children: [
      {
        path: 'detail/:id', // 设置 跳转审批详情 必传id
        name: 'approvaldetail',
        component: () => import('@/views/approval/detail.vue'),
        meta: { title: '审批详情', icon: 'user' }
      }
    ]
  },
  {
    path: '/attendance', // 考勤模块
    component: Layout,
    children: [
      {
        path: 'index', // 设置 跳转审批详情 必传id
        name: 'attendance',
        component: () => import('@/views/attendance/index.vue'),
        meta: { title: '考勤', icon: 'user' }
      }
    ]
  },
  {
    path: '/common', // 基础讲解
    component: Layout,
    children: [
      {
        path: 'index', // 设置 跳转审批详情 必传id
        name: 'common',
        component: () => import('@/views/common/index.vue'),
        meta: { keepAlive: true, title: '基础讲解1', icon: 'user' },
        children: [
          {
            path: 'one', name: 'commone', component: () => import('@/views/common/commonOne.vue'),
            hidden: true
          },
          {
            path: 'two', name: 'commtwo', component: () => import('@/views/common/commonTwo.vue'),
            hidden: true
          }
        ]
      }
    ]
  },
  {
    path: '/role',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Role',
        component: () => import('@/views/role/index'),
        meta: { title: '角色', icon: 'form' }
      }
    ]
  },
  { // 基础讲解3
    path: '/basetem',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'basetem',
        // 路由独享钩子
        // beforeEnter(to,from,next){},
        component: () => import('@/views/basetem/index'),
        meta: { title: '基础讲解3', icon: 'form' }
      }
    ]
  },
  {
    path: '/nested',
    component: Layout,
    redirect: '/nested/menu1',
    name: 'Nested',
    meta: {
      title: '售后模块',
      icon: 'nested'
    },
    children: [
      {
        path: 'menu1',
        component: () => import('@/views/nested/menu1/index'), // Parent router-view
        name: 'Menu1',
        meta: { title: '售后列表' },
        children: [
          {
            path: 'menu1-1',
            component: () => import('@/views/nested/menu1/menu1-1'),
            name: 'Menu1-1',
            meta: { title: 'Menu1-1' }
          },
          {
            path: 'menu1-2',
            component: () => import('@/views/nested/menu1/menu1-2'),
            name: 'Menu1-2',
            meta: { title: 'Menu1-2' },
            children: [
              {
                path: 'menu1-2-1',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
                name: 'Menu1-2-1',
                meta: { title: 'Menu1-2-1' }
              },
              {
                path: 'menu1-2-2',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
                name: 'Menu1-2-2',
                meta: { title: 'Menu1-2-2' }
              }
            ]
          },
          {
            path: 'menu1-3',
            component: () => import('@/views/nested/menu1/menu1-3'),
            name: 'Menu1-3',
            meta: { title: 'Menu1-3' }
          }
        ]
      },
      {
        path: 'menu2',
        component: () => import('@/views/nested/menu2/index'),
        name: 'Menu2',
        meta: { title: 'menu2' }
      }
    ]
  },

  {
    path: 'external-link',
    component: Layout,
    children: [
      {
        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: { title: 'External Link', icon: 'link' }
      }
    ]
  },
  { // 基础讲解
    path: '/base',
    component: Layout,
    children: [ // 子路由的写法  path 不加/
      {
        path: 'index',
        name: 'BaseIndex',
        component: () => import('@/views/base/index'),
        meta: { title: '基础讲解', icon: 'el-icon-s-home' }
      }
    ]

  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'hash', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
